package com.sinodata.ns.sv;

import jt.classic.system.ISystem;
import jt.classic.system.database.SystemDataSource;

import org.limp.basework.SimpleBean4Table;
import org.limp.basework.impl.SimpleBean4TableImpl;
import org.limp.basework.tools.BaseworkUtil;

import org.limp.mine.DataTrimmer;

import java.sql.Connection;

import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;


public class SupervisorLoadTimer {
    private Timer timer = new Timer();
    private long delay = 5000;
    private long period = 10000;

    /**
     * 构造函数
     * @param delay 延时时间
     * @param period 间隔时间
     */
    public SupervisorLoadTimer(long delay, long period) {
        if (delay > 0) {
            this.delay = delay;
        }

        if (period > 0) {
            this.period = period;
        }
    }

    /**
     * 启动任务计划
     */
    public void runTask() {
        System.out.println("system message: SupervisorLoad task is running...");
        timer.schedule(new MyTask(), this.delay, this.period);
    }

    /**
     * 停止任务计划
     */
    public void stopTask() {
        System.out.println(
            "system message: SupervisorLoad task is cancelled...");
        timer.cancel();
    }

    /**
     * 任务接口实现类
     * @author mido
     *
     */
    private class MyTask extends TimerTask {
        public void run() {
            Connection conn = null;

            try {
                SystemDataSource ds = SystemDataSource.getInstance();
                conn = ds.getConnection();

                DataTrimmer trimmer = new DataTrimmer(conn);

                /*同步系统用户为监理人员*/
                String sql = "SELECT ID FROM ISYS_USER WHERE ID NOT IN (SELECT NU_USERID FROM APP_NS_JLRY) AND (RATING = 2 OR RATING = 1) AND FLAG > -1 AND LOGID <> 'admin'";

                List list = trimmer.searchMultiData(sql);

                if ((list != null) && !list.isEmpty()) {
                    BaseworkUtil util = new BaseworkUtil();

                    for (int i = 0; i < list.size(); i++) {
                        Map info = (Map) list.get(i);

                        String id = (String) info.get("ID");

                        SimpleBean4Table sbt = new SimpleBean4TableImpl(
                                "APP_NS_JLRY");

                        sbt.getResource().put("nuserid", id);
                        sbt.getResource().put("nubdid", '0');

                        util.manualSave1(sbt, conn);
                    }
                }
                
                /*删除无效的监理人员(监理人员与系统用户失去关联)*/
                String sql2 = "DELETE FROM APP_NS_JLRY WHERE NU_USERID NOT IN (SELECT ID FROM ISYS_USER)";
                
                trimmer.execute(sql2);

                conn.commit();
            } catch (Exception ex) {
                try {
                    conn.rollback();
                } catch (Exception ex1) {
                    ISystem.catchException(this, ex1);
                }

                ISystem.catchException(this, ex);
            } finally {
                SystemDataSource.closeConnection(conn);
            }
        }
    }
}
